--  --------------------------------------------------------------------------
--  @File:        messages.adb
--
--  @Project:     Softwarequalitaet Blatt 4 / Aufgabe 8
--  @Version:     1.0
--  @Created:     2012-24-01
--  @Author:      Benedikt Markt, Aleksander Zotov
--
--  @Description: Package for creating a list of message pairs;
--
--  @Modifications:

with Pair;
with List;

package body Messages is

   package Lists is new List (Pair.Pair_Type);

   L : Lists.List_Type;

   --  -----------------------------------------------------------------------
   --  @Procedure: Add_Message
   --
   --  @Description: Adds a message with its key to this ADO;
   --  @Parameter  : +Key: key of the message;
   --                +Msg: the message;
   procedure Add_Message (Key : in String; Msg : in String) is
   begin
      Lists.Append (L, Pair.Create (Key, Msg));
   end Add_Message;

   --  -----------------------------------------------------------------------
   --  @Function: Has_Message
   --
   --  @Description: Determines if for the given key a message is stored.
   --  @Parameter  : +Key: the key;
   --  @Return     : True if there is a message for key, otherwise false;
   function Has_Message (Key : in String) return Boolean is
   begin
      for I in 1 .. Lists.Length (L) loop
         if Pair.Key (Lists.Value (L, I)) = Key then
            return True;
         end if;
      end loop;
      return False;
   end Has_Message;

   --  -----------------------------------------------------------------------
   --  @Function: Get_Message
   --
   --  @Description: Returns the message stored for the given key;
   --  @Parameter  : +Key: the key;
   --              : +Default_Msg: default message;
   --  @Return     : the message stored for the key;
   function Get_Message (Key         : in String;
                         Default_Msg : in String := "") return String is
   begin
      if Has_Message (Key) then
         for I in 1 .. Lists.Length (L) loop
            if Pair.Key (Lists.Value (L, I)) = Key then
               return Pair.Value (Lists.Value (L, I));
            end if;
         end loop;
      end if;
      return Default_Msg;
      end Get_Message;
end Messages;
